//
//  WDKeyListController.m
//  OpenSesame
//
//  Created by zhangjiaming on 17/5/10.
//  Copyright © 2017年 OpenSesame. All rights reserved.
//

#import "WDKeyListController.h"
#import "WDKeyViewCell.h"
#import "WDKey.h"
#import "JMKeyInforController.h"
#import "WDKeyInfoController.h"
#define KEYLIST @"http://47.92.139.238/copying/index/keyList"
@interface WDKeyListController ()
@property (nonatomic,strong)NSArray *dataArray;
@end

@implementation WDKeyListController
#pragma  mark 懒加载
-(NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _name;
    [self.tableView registerNib:[UINib nibWithNibName:@"WDKeyViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"keylist"];
    [self getdata];
    // 一句话设置隐藏多余的线
   self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

-(void)getdata{
    NSUserDefaults *defult =[NSUserDefaults standardUserDefaults];
    WDLog(@"%@",[defult objectForKey:@"token"]);
    NSDictionary *dic = @{@"token":[defult objectForKey:@"token"],
                          @"carId":_carID,
                          };
    [JMHttpTool post:KEYLIST param:dic success:^(id responseObject) {
        NSDictionary *dict =[NSDictionary dictionaryWithDictionary:responseObject];
        if ([dict[@"code"] isEqualToString:@"200"]) {
              _dataArray = [WDKey mj_objectArrayWithKeyValuesArray:dict[@"data"]];
            WDLog(@"%@",_dataArray);
            [self.tableView reloadData];
        }
      
        
    } frail:^(NSError *error) {
        
        
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WDKeyViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"keylist" forIndexPath:indexPath];
    WDKey *model = _dataArray[indexPath.row];
    //cell.keyModel = model;
    NSString *URL = [NSString stringWithFormat:@"%@%@",BASEURL,model.pic];
    [cell.keyImageView sd_setImageWithURL:[NSURL URLWithString:URL] placeholderImage:[UIImage imageNamed:@"x7.png"]];
    cell.nameLabel.text = model.name;
    cell.rateLabel.text = model.frequency;
    cell.contentLabel.text = model.content;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
     WDKey *model = _dataArray[indexPath.row];
    
    //JMKeyInforController *infoVC =[[JMKeyInforController alloc]init];
       WDKeyInfoController *infoVC =[[WDKeyInfoController alloc]init];
    infoVC.nameStr = model.name;
    infoVC.contentStr = model.content;
    infoVC.fitStr = model.fit;
    infoVC.dateStr = model.frequency;
    infoVC.keyId = model.keyId;
    
 
    [self.navigationController pushViewController:infoVC animated:YES];
}

@end
